<?php
/*
function callpay
function js
function jsApiCall
function notify
function ok
function submit

占位
.#.#.#.#.#.#.#.#.#.#*/
//占位
abstract class class_wxpay
{
	var $appid;	//appid
	var $mchid;	//帐号
	var $mchkey;	//安全检验码
	var $urlok;	//成功后返回的url
	
	abstract protected function ok($array);
	//---------------------------------------------------------------------------------------
	protected function init()
	{
		$this->appid = $GLOBALS['WXAPPID'];
		$this->mchid = $GLOBALS['WXMCHID'];
		$this->mchkey = $GLOBALS['WXMCHSECRET'] ? $GLOBALS['WXMCHSECRET'] : $GLOBALS['WXMCHKEY'];
		$this->urlok = $GLOBALS['DOMAIN'] . '/index.php/wxpay';
	}
	
	//---------------------------------------------------------------------------------------
	function notify()
	{
		$this->init();
		if($this->appid == '' || $this->mchid == '' || $this->mchkey == '')
			return false;
		
		$str = file_get_contents('php://input');
		$array = wx_parseXML($str);
		
		if($array['return_code'] != 'SUCCESS')
			return;
		
		if(!ctype_alnum($array['nonce_str']))
			return;
		
		if($array['out_trade_no'] == '')
			return;
		
		$re = array(
			'return_code' => 'SUCCESS',
			'return_msg' => 'OK',
		);
		
		echo wx_xmlencode($re);
		
		$orderid = $array['out_trade_no'];
		$params = array();
		$params['appid'] = $this->appid;
		$params['mch_id'] = $this->mchid;
		$params['nonce_str'] = _only();
		$params['out_trade_no'] = $orderid;
		$xml = wx_payxml($params, $this->mchkey);			
		$url = 'https://api.mch.weixin.qq.com/pay/orderquery';
		$curl = NEW class_curl;
		$xml = $curl->post($url, $xml);
		$new = wx_parseXML($xml);
		
		if($new['trade_state'] === 'SUCCESS')
		{
			$tmp = array('id' => $orderid, 'trade_no' => $new['transaction_id']);
			$this->ok($tmp);
		}
	}
	//---------------------------------------------------------------------------------------
	//微信浏览器支付
	function submit_jsapi($orderid, $subject, $total_fee, $openid, $urls)
	{
			$this->init();
			if($this->appid == '' || $this->mchid == '' || $this->mchkey == '')
				return false;
		
			$params = array();
			$params['appid'] = $this->appid;
			$params['mch_id'] = $this->mchid;
			$params['nonce_str'] = _only();
			$params['body'] = $subject;
			$params['out_trade_no'] = $orderid;
			$params['trade_type'] = "JSAPI";
			$params['total_fee'] = floor($total_fee * 100);
			$params['spbill_create_ip'] = $_SERVER['REMOTE_ADDR'];
			$params['notify_url'] = $this->urlok;
			$params['openid'] = $openid;
			
			$array = $this->_unifiedorder($params);
			$json = $this->_jsapi($array);
			if($json == false)
			{
				_log1($array);
				return false;
			}
						
			return $this->_js($json, $urls);
	}
	//---------------------------------------------------------------------------------------
	//PC网站扫码支付
	function submit_native($orderid, $subject, $total_fee)
	{
			$this->init();

			if($this->appid == '' || $this->mchid == '' || $this->mchkey == '')
				return false;

			$tmp = rand(100000000,999999999);
			
			$params = array();
			$params['appid'] = $this->appid;
			$params['mch_id'] = $this->mchid;
			$params['nonce_str'] = $tmp;
			$params['body'] = $subject;
			$params['out_trade_no'] = $orderid;
			$params['trade_type'] = "NATIVE";
			$params['total_fee'] = floor($total_fee * 100);
			$params['spbill_create_ip'] = $_SERVER['SERVER_ADDR'];
			$params['notify_url'] = $this->urlok;
			$params['product_id'] = "product_{$tmp}";
			
			$array = $this->_unifiedorder($params);
			$codeurl = $array['code_url'];
			if($codeurl == '')
			{
				return false;
			}
			
			return $codeurl;
	}
	//---------------------------------------------------------------------------------------
	//小程序 小游戏
	function submit_xyx($orderid, $subject, $total_fee, $openid)
	{
			$this->init();
			if($this->appid == '' || $this->mchid == '' || $this->mchkey == '')
				return false;

			$tmp = rand(100000000,999999999);
			
			$params = array();
			$params['appid'] = $this->appid;
			$params['mch_id'] = $this->mchid;
			$params['nonce_str'] = _only();
			$params['body'] = $subject;
			$params['out_trade_no'] = $orderid;
			$params['trade_type'] = "JSAPI";
			$params['total_fee'] = floor($total_fee * 100);
			$params['spbill_create_ip'] = $_SERVER['REMOTE_ADDR'];
			$params['notify_url'] = $this->urlok;
			$params['openid'] = $openid;
			
			$array = $this->_unifiedorder($params);
			return $array;
	}
	//---------------------------------------------------------------------------------------
	//统一下单
	function _unifiedorder($params)
	{
			$xml = wx_payxml($params, $this->mchkey);
			$url = 'https://api.mch.weixin.qq.com/pay/unifiedorder';
			$curl = NEW class_curl;
			$xml = $curl->post($url, $xml);
			$array = wx_parseXML($xml);
			
			return $array;
	}
	//---------------------------------------------------------------------------------------
	//jsapi支付专用
	protected function _jsapi($new)
	{
		if(empty($new))
		{
			//$err = 'xml解释错误';
			return false;
		}
		
		if($new['return_code'] !== 'SUCCESS' || $new['prepay_id'] == '')
		{
			//$err = $new['err_code_des'];
			return false;
		}
	
		$json = array();
		$json['appId'] = $new['appid'];
		$json['timeStamp'] = (string)$GLOBALS['TIME'];
		$json['nonceStr'] = _only();
		$json['package'] = 'prepay_id='.$new['prepay_id'];
		$json['signType'] = 'MD5';
		
		$sign = wx_sign($json, $this->mchkey);
		$json['paySign'] = $sign;
	
		return json_encode($json);
	}
	//---------------------------------------------------------------------------------------
	//jsapi支付专用
	protected function _js($json, $jsParams)
	{
		return '<script type="text/javascript">
			function jsApiCall()
			{
				WeixinJSBridge.invoke(
					"getBrandWCPayRequest",
					' . $json . ',
					'. $this->_jsfunc($jsParams[0], $jsParams[1]) .'
				);
			}
		
			function callpay()
			{
				if (typeof WeixinJSBridge == "undefined"){
				    if( document.addEventListener ){
				        document.addEventListener("WeixinJSBridgeReady", jsApiCall, false);
				    }else if (document.attachEvent){
				        document.attachEvent("WeixinJSBridgeReady", jsApiCall); 
				        document.attachEvent("onWeixinJSBridgeReady", jsApiCall);
				    }
				}else{
				    jsApiCall();
				}
			}
			
			window.onload = function(){
				callpay();
			};
		</script>';
	}
	//---------------------------------------------------------------------------------------
	//jsapi支付专用
	function _jsfunc($url_true, $url_false)
	{
		$jsfunc = '
function(res)
{
	if(res.err_msg == "get_brand_wcpay_request:ok")
	{
		alert("支付成功");
		var url = "'.$url_true.'";
		window.location.href = url;
	}
	else
	{
		alert("支付失败");
		//alert(res.err_msg);
		var url = "'.$url_false.'";
		window.location.href = url;
	}
}
';

		return $jsfunc;
	}
}